/*
 * 存放未访问url
 */
package util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class RedisAPI {
	private static JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");


	public void deleteKey(String key) {
		Jedis jedis = pool.getResource();
		try {
			jedis.del(key);
		} catch (JedisConnectionException e) {
			if(null != jedis) {
				pool.returnBrokenResource(jedis);
				jedis = null;
			}
		}finally {
			if(null != jedis) {
				pool.returnResource(jedis);
			}
			
		}
		
	}
	public void destoryPool() {
		pool.destroy();
	}
	public void add(String key,String members) {
		Jedis jedis = pool.getResource();
		try {
			jedis.sadd(key, members);
		} catch (JedisConnectionException e) {
			if(null != jedis) {
				pool.returnBrokenResource(jedis);
				jedis = null;
			}
		}finally {
			if(null != jedis) {
				pool.returnResource(jedis);
			}
		}
	}
	
	public boolean isEmpty(String key) {
		Jedis jedis = pool.getResource();
		boolean result = true;
		try {
			
			//判断是否存在
			result=!jedis.exists(key); 
		} catch (JedisConnectionException e) {
			if(null != jedis) {
				pool.returnBrokenResource(jedis);
				jedis = null;
			}
		}finally {
			if(null != jedis) {
				pool.returnResource(jedis);
			}
		}
		
		return result; 
	}
	
	public String remove(String key) {
		
		Jedis jedis = pool.getResource();
		String result=null;
		try {
			
			//随机返回并删除名称为key的set中一个元素
			result=jedis.spop(key);
		} catch (JedisConnectionException e) {
			if(null != jedis) {
				pool.returnBrokenResource(jedis);
				jedis = null;
			}
		}finally {
			if(null != jedis) {
				pool.returnResource(jedis);
			}
		}
		return result;
	}
	public long size(String key) {
		Jedis jedis = pool.getResource();
		long result = 0;
		try {
			
			//随机返回并删除名称为key的set中一个元素
			result=jedis.scard(key);
		} catch (JedisConnectionException e) {
			if(null != jedis) {
				pool.returnBrokenResource(jedis);
				jedis = null;
			}
		}finally {
			if(null != jedis) {
				pool.returnResource(jedis);
			}
		}
		return result;
	}
	public boolean contains(String key,String member) {
		Jedis jedis = pool.getResource();
		boolean result = true;
		try {
			
			//随机返回并删除名称为key的set中一个元素
			result=jedis.sismember(key, member) ;
		} catch (JedisConnectionException e) {
			if(null != jedis) {
				pool.returnBrokenResource(jedis);
				jedis = null;
			}
		}finally {
			if(null != jedis) {
				pool.returnResource(jedis);
			}
		}
		
		return result; 
	}
	//test
	public static void main(String args[]) throws InterruptedException {
		RedisAPI a= new RedisAPI();
		/*a.add("visitedUrl","www.baidu.com");
		a.add("visitedUrl","www.baidu.co");
		a.add("visitedUrl","www.baidu.om");
		a.remove("visitedUrl");
		System.out.println(a.isEmpty("visitedUrl"));
		a.remove("visitedUrl");
		System.out.println(a.isEmpty("visitedUrl"));
		a.remove("visitedUrl");
		System.out.println(a.isEmpty("visitedUrl"));*/
		a.add("visitedUrl","www.baidu.om");
		System.out.println(a.contains("visitedUrl", "www"));
		System.out.println(a.contains("visitedUrl", "www.baidu.om"));
		System.out.println(a.isEmpty("visitedUrl"));
	}
}
 